import queue
import re
import threading


class StreamLogHandler:
    def __init__(self):
        self.log_queue = queue.Queue()
        self._lock = threading.Lock()
        # 用于解析日志级别的正则表达式
        self.log_level_re = re.compile(r'\b(INFO|WARNING|ERROR|DEBUG|CRITICAL)\b')

    def write(self, message):
        with self._lock:
            # 保留原始格式的完整日志消息
            self.log_queue.put(message)

    def get_logs(self):
        logs = []
        while not self.log_queue.empty():
            logs.append(self.log_queue.get())
        return logs